package org.my.stock.util;

import java.beans.PropertyVetoException;
import java.sql.*;

/**
 * my_stock 表的操作
 * my_stock_type 表的操作
 * */
public class MyStockUtil {

    /**
     * 判断股票是否已经存在股票表中了
     * */
    public static boolean has_stock(final String symbol, final String name, final String type_name) throws PropertyVetoException, SQLException, ClassNotFoundException {
        Connection conn = MyUtil.getConnection();
        boolean flag = false;
        try {
            PreparedStatement statement = conn.prepareStatement("select m.symbol as symbol from my_stock as m where m.symbol = ?");
            statement.setString(1, symbol);
            ResultSet rs = statement.executeQuery();
            while (rs.next())
            {
                flag = true;
                break;
            }
            rs.close();
            statement.close();

            if (flag == false)
            {
                PreparedStatement st = conn.prepareStatement("insert into my_stock (symbol, name) values (?, ?)");
                st.setString(1, symbol);
                st.setString(2, name);
                st.executeUpdate();

                st = conn.prepareStatement("insert into my_stock_type (symbol, type_name) values (?, ?)");
                st.setString(1, symbol);
                st.setString(2, type_name);
                st.executeUpdate();

                st.close();
                flag = true;
            }
        }
        catch (Exception e)
        {
            throw e;
        }
        finally {
            conn.close();
        }

        return flag;
    }

    /**
     * 判断股票是否已经存在股票表中了
     * */
    public static void has_stock_type(final String symbol, final String type_name) throws PropertyVetoException, SQLException, ClassNotFoundException {
        Connection conn = MyUtil.getConnection();
        boolean flag = false;
        try {
            PreparedStatement statement = conn.prepareStatement("select m.id as id from my_stock_type as m where m.symbol = ? and m.type_name = ?");
            statement.setString(1, symbol);
            statement.setString(2, type_name);
            ResultSet rs = statement.executeQuery();
            while (rs.next())
            {
                flag = true;
                break;
            }

            if (flag == false)
            {
                PreparedStatement st = conn.prepareStatement("insert into my_stock_type (symbol, type_name) values (?, ?)");
                st.setString(1, symbol);
                st.setString(2, type_name);
                st.executeUpdate();
            }
        }
        catch (Exception e)
        {
            throw e;
        }
        finally {
            conn.close();
        }
    }

    /**
     * 添加股票和股票分类
     * */
    public static void insert_stock(final String symbol, final String name, final String type_name) throws PropertyVetoException, SQLException, ClassNotFoundException {

        if (has_stock(symbol, name, type_name) == false)
        {
            has_stock_type(symbol, type_name);
        }
    }

}








































